package com.gson;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class TestGson {

    public static void main(String[] args) {
        // data
        String jsonStr = "{\"name\":\"tiesheng\",\"age\":[22,33,44,55]}";
        String jsonStr1 = "{'outer_id':'9195293333','pic_url':'http://img01.taobaocdn.com/bao/uploaded/i4/T1UmNLXhpnXXah2b75_054841.jpg','outer_num_id':'919529(123123!*$(*(@$*@)','match_pic_id':'0','match_pic_title':'','match_pic_url':'','match_score':'1','featnum':'0','user_strid':'68b36d0fdb72224b3bd5565fd8aed7c5'}";
        String gsonStr = "[{'ip':'10.250.4.98', 'area':[1,2,3,4]}]";

        // covert
        Gson gson = new Gson();

        // Type type = new TypeToken<Map<String, Object>>() {
        // }.getType();
        // Map<String, Object> result = gson.fromJson(jsonStr, type);
        // System.out.println(result);
        //
        // Map<String, Object> rs_01 = gson.fromJson(jsonStr1, type);
        // System.out.println(rs_01);
        // System.out.println("outer_id=" + rs_01.get("outer_id"));
        //
        // Type typeArr = new TypeToken<List<TempDO>>() {
        // }.getType();
        // List<TempDO> rs_02 = gson.fromJson(gsonStr, typeArr);
        // System.out.println(rs_02);
        //
        TempDO testDate = new TempDO();
        testDate.setBirthday(new Date());
        testDate.setIp("127.0.0.1");

        Type tempDOType = new TypeToken<TempDO>() {
        }.getType();
        System.out.println(gson.toJson(testDate, tempDOType));
        String dateStr = "{\"birthday\":\"2012-07-03 00:00:00\",\"ip\":\"127.0.0.1\"}";
        System.out.println(gson.fromJson(dateStr, tempDOType));

    }

}

class TempDO implements Serializable {
    private static final long serialVersionUID = -965663783880337591L;

    private Date              birthday;
    private String            ip;
    private List<Long>        area;

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public List<Long> getArea() {
        return area;
    }

    public void setArea(List<Long> area) {
        this.area = area;
    }

    @Override
    public String toString() {
        return "ip=" + ip + ", ins=" + area + ", date=" + birthday;
    }
}
